﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Domain.Repository;
using NLite.Domain.Model;
using NLite.Data;

namespace NLite.Domain.Service
{
    /// <summary>
    /// 实体服务接口，提供最基本的实体服务诸如增删改查
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TId"></typeparam>
    [Contract]
    public interface IEntityService<T, TId> : IDomainService where T : class,IEntity<TId> 
    {

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        void Add(T entity);

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        void Update(T entity);

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="ids"></param>
        void Delete(params TId[] ids);

        /// <summary>
        /// 通过主键Id得到对应的实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(TId id);
        /// <summary>
        /// 返回所有的实体列表
        /// </summary>
        /// <returns></returns>
        IQueryable<T> List();

        /// <summary>
        /// 分页、搜索、排序查询
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        IPagination<T> Query(QueryContext ctx);
    }

    /// <summary>
    /// 主键类型是长整型的实体服务接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Contract]
    public interface IEntityService<T> : IEntityService<T, long> where T : class,IEntity<long> { }

    /// <summary>
    /// 实体服务
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class EntityService<T> : DomainService, IEntityService<T> where T :class, IEntity<long>
    {
        /// <summary>
        /// 仓储
        /// </summary>
        [Inject]
        protected IRepository<T> Repository { get; set; }

        /// <summary>
        /// 返回所有的实体列表
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<T> List()
        {
            return Repository.Query();
        }


        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Add(T entity)
        {
            Repository.Add(entity);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Update(T entity)
        {
            Repository.Update(entity);
        }

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="ids"></param>
        public virtual void Delete(params long[] ids)
        {
            Repository.Delete(ids);
        }

        /// <summary>
        /// 通过主键Id得到对应的实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T Get(long id)
        {
            return Repository.Get(id);
        }

        /// <summary>
        /// 分页、搜索、排序查询
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public virtual IPagination<T> Query(QueryContext ctx)
        {
            return Repository.Query(ctx);
        }
    }
}
